package cn.lxg.utils;

import cn.lxg.dao.entity.SuperLottery;
import cn.hutool.core.collection.CollectionUtil;

import java.util.*;

public class SuperLotteryUtils {

    private static final Random random = new Random();

    /**
     * 获取不能出现的数字
     *
     * @param nums
     * @param thirtyByWeek
     * @param period
     * @param type         0红球 1蓝色球
     * @return
     */
    public static Set<Integer> getCannotAppearNum(List<Integer> nums, List<SuperLottery> thirtyByWeek, int period, int type) {
        // 使用 HashMap 来记录每个数字的出现频率
        Map<Integer, Integer> frequencyMap = new HashMap<>();

        for (int num : nums) {
            frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
        }
        //获取最多次数最
        Integer max = CollectionUtil.max(frequencyMap.values());
        List<Integer> temp = new ArrayList<>(frequencyMap.values());
        while (temp.contains(max)) {
            temp.remove(max);
        }

        //最接近的次数
        int closestValue = RangeUtil.findClosestValue(temp, max);
        //最近多少期
        List<SuperLottery> lotteries = CollectionUtil.split(thirtyByWeek, period).get(0);

        //返回数组
        Set<Integer> result = new HashSet<>();
        for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
            if (entry.getValue().equals(max)) {
                if (random.nextInt(3) == 1) {
                    result.add(entry.getKey());
                }
                continue;
            }
            //最接近且最近出现的
            if (entry.getValue().equals(closestValue)) {
                int i = random.nextInt(5);
                if (i != 1) {
                    continue;
                }

                for (SuperLottery lottery : lotteries) {
                    if (type == 0) {
                        if (Objects.equals(lottery.getRed1(), entry.getKey()) || Objects.equals(lottery.getRed2(), entry.getKey()) || Objects.equals(lottery.getRed3(), entry.getKey())
                                || Objects.equals(lottery.getRed4(), entry.getKey()) || Objects.equals(lottery.getRed5(), entry.getKey())
                        ) {
                            result.add(entry.getKey());
                        }
                    } else {
                        if (Objects.equals(lottery.getBlue1(), entry.getKey()) || Objects.equals(lottery.getBlue2(), entry.getKey())) {
                            result.add(entry.getKey());
                        }
                    }
                }
            }
        }
        return result;
    }

}
